package adfc2.actions;

import adfc2.core.Creature;
import adfc2.core.World;

public class StepAction extends LongAction {
    public final int time;
    public final int toxy;


    public StepAction(Creature actor, int dx, int dy) {
        super(actor);
        if (dx > 1) dx = 1;
        if (dx < -1) dx = -1;
        if (dy > 1) dy = 1;
        if (dy < -1) dy = -1;
        this.time = (dx == 0 || dy == 0) ? 1000 : 1500;
        toxy = World.mkxy(actor.getX() + dx, actor.getY() + dy);
    }

    public StepAction(Creature actor, int toxy) {
        super(actor);
        this.toxy = toxy;
        this.time = (actor.getX() == World.xy2x(toxy) || actor.getY() == World.xy2y(toxy)) ? 1000 : 1500;
    }

    @Override
    protected void started() {
        assert World.near(actor.getXY(), toxy);
        assert (actor.container == null);
        if ((World.world().blocks[toxy] & World.MASK_IMPASSABLE) != 0) {
            failed();
        }
    }

    @Override
    protected boolean progressing(long dt) {
        return (totaltime > time);
    }

    @Override
    protected void finished() {
        actor.tpTo(toxy);
    }

    @Override
    protected void interrupted() {
        // do nothing, just stay here
    }

    @Override
    public double estimateProgress() {
        return ((double) totaltime) / time;
    }

    @Override
    public String toString() {
        return "StepAction[" + World.xy2x(toxy) + "," + World.xy2y(toxy) + "]";
    }
}
